<template>
  <Dropdown style="float: right" @on-click="handleDropdown">
    <div>
      <Avatar icon="ios-person" />
      {{ userName }}
    </div>
    <DropdownMenu slot="list">
      <DropdownItem name="prtsonalInfo">个人信息</DropdownItem>
      <DropdownItem name="editPwd">修改密码</DropdownItem>
      <DropdownItem name="loginOut">退出登录</DropdownItem>
    </DropdownMenu>
  </Dropdown>
</template>

<script>
  import qs from 'qs'
  import request from '../utils/request'
  export default {
    computed: {
      userName() {
        const cookie = document.cookie || '';
        const cookieStr = cookie.replace(/; /gm, '&');
        const cookieObj = qs.parse(cookieStr);
        const userName =  cookieObj.userName || '--'
        return userName;
      }
    },
    methods: {
      handleDropdown(name) {
        if (name === 'loginOut') {
          request({
            url: '/user/loginOut'
          }).then((result) => {
            const { code, message } = result;
            if (code === 200) {
              this.$Message.success(message);
              this.$router.push({name: 'Login'})
            }
          }).catch((err) => {
            console.log(err);
          });
        }
      }
    },
  }
</script>

<style scoped>

</style>